﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;

namespace ProxyHero.Loading
{
    public delegate void DelegateCloseSplash();

    public partial class Splash : Form
    {
        #region property
        private Color _lineColor = Color.Black;
        /// <summary>
        /// 画线颜色
        /// </summary>
        [Description("画线颜色"), Category("Appearance")]
        public Color LineColor
        {
            get { return _lineColor; }
            set { _lineColor = value; }
        }
        private float _lineWidth = 2;
        /// <summary>
        /// 画线的宽度
        /// </summary>
        [Description("画线的宽度"), Category("Appearance")]
        public float LineWidth
        {
            get { return _lineWidth; }
            set { _lineWidth = value; }
        }
        #endregion

        #region Private variables
        private static Splash m_instance;
        private static Form m_mainForm;
        private static string m_text;
        private DelegateCloseSplash m_delegateClose;
        private Graphics graphics;
        private Pen myPen;
        #endregion

        public Splash()
        {
            InitializeComponent();

            this.ShowInTaskbar = false;
            this.TopMost = true;

            // tie up the events
            this.KeyUp += new System.Windows.Forms.KeyEventHandler(this.SplashForm_KeyUp);
            //this.MouseDown += new MouseEventHandler(SplashForm_MouseClick);
            this.label1.Text = m_text;
            this.pictureBox1.Left = (this.Width - this.pictureBox1.Width - this.label1.Width) / 2;
            this.label1.Left = this.pictureBox1.Left + this.pictureBox1.Width;

            this.Left = m_mainForm.Left + (m_mainForm.Width-this.Width) / 2;
            this.Top = m_mainForm.Top + (m_mainForm.Height - this.Height) / 2;
            // thread handling
            m_delegateClose = new DelegateCloseSplash(InternalCloseSplash);

        }

        protected override void OnPaint(PaintEventArgs e)
        {


            myPen = new Pen(this.LineColor, this.LineWidth);
            graphics = this.CreateGraphics();

            graphics.DrawRectangle(myPen, this.ClientRectangle);
            base.OnPaint(e);
        }

        #region Event Handlers

        void SplashForm_MouseClick(object sender, MouseEventArgs e)
        {
            this.InternalCloseSplash();
        }

        private void SplashForm_KeyUp(object sender, System.Windows.Forms.KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Escape)
                this.InternalCloseSplash();
        }
        #endregion // Event Handlers

        #region Threading code
        // ultimately this is called for closing the splash window
        void InternalCloseSplash()
        {
            this.Close();
            this.Dispose();
        }
        // this is called by the new thread to show the splash screen
        private static void MySplashThreadFunc()
        {
            m_instance = new Splash();
            m_instance.TopMost = false;
            m_instance.ShowDialog();
        }
        #endregion // Multithreading code

        #region Public methods
        // this can be used for About dialogs
        public static void ShowModal(Form mainForm, string text)
        {
            m_mainForm = mainForm;
            m_text = text;
            MySplashThreadFunc();
        }
        // Call this method with the image file path and the color 
        // in the image to be rendered transparent
        public static void StartSplash(Form mainForm,string text)
        {
            m_mainForm = mainForm;
            m_text = text;
            // Create and Start the splash thread
            Thread InstanceCaller = new Thread(new ThreadStart(MySplashThreadFunc));
            InstanceCaller.Start();
        }

        // Call this at the end of your apps initialization to close the splash screen
        public static void CloseSplash()
        {
            if (m_instance != null)
                m_instance.Invoke(m_instance.m_delegateClose);

        }
        #endregion // Public methods
    }
}
